<template>
  <bodyWrap :isShow="bodyShow" style="z-index: 99">
    <div slot="body" v-if="bodyShow">
      <searchWrap @inputValue="inputValue"></searchWrap>
      <ul class="contacts-ul" v-if="searchList.length">
        <li class="contacts-li"
            v-for="p in searchList">
          <img src="../../img/org.png" class="img-h">
          <input class="checkInput"
                 :value="p"
                 type="checkbox"
                 v-model="checked">
          <div class="txtWrap">
            <div class="name">
              {{p.orgName}}
            </div>
            <div class="checkIcon"></div>
          </div>
        </li>
      </ul>
    </div>
  </bodyWrap>
</template>

<script>
  import searchWrap from "./search.vue"
  export default {
    data() {
      return {
        bodyShow: false,
        checked: [],
        searchList: [],
        headerRightType: {},
        headerFlag: "",
        id: "",
      }
    },
    methods: {
      init() {
        this.checked = _data.orgChecked;
        this.headerRightType = this.$store.state.headerRightType;
        this.headerFlag = this.$store.state.showFlag;
        this.code = _methods.getUrlParam("code") - 0;
        switch (this.code) {
          case 3:
            this.checked = _data.orgChecked;
            break;
          case 4:
            break;
          case 5:
            this.checked = _data.csChecked;
            break;
        }
        _methods.headerTitleChange(111);
        _methods.headerRightShow({
          val: 111,
          txt: "确定"
        });
        if (_data.userInfo.roleRank == 3) {
          this.listOperators();
        } else {
          this.organizeTable();
        }
      },
      organizeTable(){
        _ajax.noLoading(window._ajaxUrls.organizeTable.url + `?id=${this.code}`,
          {},
          (ajData) => {
            let rs = ajData.result;
            _data.orgList = rs;
            this.searchList = rs;
            this.bodyShow = true;
          },
          window._ajaxUrls.organizeTable.type);
      },
      listOperators(){
        _ajax.noLoading(window._ajaxUrls.listOperators.url,
          {orgID: _data.userInfo.deptId},
          (ajData) => {
            let rs = ajData.result;
            _data.orgList = rs;
            this.searchList = rs;
            this.bodyShow = true;
          },
          window._ajaxUrls.listOperators.type);
      },
      inputValue(v) {
        let k = v.toString().trim();
        if (k != "") {
          this.searchList = _data.leaderList.filter((v) => {
            return v.orgName.indexOf(k) != -1;
          });
        } else {
          this.searchList = _data.leaderList;
        }
      },
    },
    components: {
      searchWrap
    },
    created() {
      this.init();
    },
    mounted() {
      let self = this;
      _data.busInit.on_(() => {
        let da = {
          txt: "",
          val: ""
        };
        let key = [];
        let txt = [];
        self.checked.forEach((i) => {
          key.push(i.orgID);
          txt.push(i.orgName);
        });
        da.txt = txt.join(",");
        da.val = this.checked;
        //5 安全厂商 3地方网信办
        switch (this.code) {
          case 3:
            _data.orgChecked = this.checked;
            if (da.txt) {
              self.$store.commit("_orgChecked", da);
            } else {
              self.$store.commit("_orgChecked", {
                txt: "点击选择响应单位",
                val: []
              });
            }
            break;
          case 4:
            if (da.txt) {
              self.$store.commit("_jqChecked", da);
            } else {
              self.$store.commit("_jqChecked", {
                txt: "点击选择下发单位",
                val: []
              });
            }
            break;
          case 5:
            _data.csChecked = this.checked;
            if (da.txt) {
              self.$store.commit("_dwChecked", da);
            } else {
              self.$store.commit("_dwChecked", {
                txt: `点击选择安全厂商`,
                val: []
              });
            }
            break;
        }
        _methods.goBack();
      });
    },
    beforeDestroy() {
      _data.busInit.off_();
      _methods.headerTitleChange(this.headerFlag, true);
      _methods.headerRightShow(this.headerRightType);
    }
  }
</script>
